package com.lizheng.yunservice.travel.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lizheng.yunservice.common.domain.PageQuery;
import com.lizheng.yunservice.common.page.TableDataInfo;
import com.lizheng.yunservice.travel.doamin.vo.VtpCustomerLeaveMsgPageResponse;
import com.lizheng.yunservice.travel.doamin.vo.VtpCustomerLeaveMsgVo;
import com.lizheng.yunservice.travel.doamin.VtpCustomerLeaveMsg;
import com.lizheng.yunservice.travel.dto.VtpCustomerLeaveMsgDto;
import com.lizheng.yunservice.travel.mapper.VtpCustomerLeaveMsgMapper;
import com.lizheng.yunservice.travel.service.IVtpCustomerLeaveMsgService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 客户留言服务实现类
 *
 * @author lizheng
 * &#064;date  2024-06-06
 */
@Service
@RequiredArgsConstructor
public class VtpCustomerLeaveMsgServiceImpl implements IVtpCustomerLeaveMsgService {

    private final VtpCustomerLeaveMsgMapper vtpCustomerLeaveMsgMapper;

    @Override
    public List<VtpCustomerLeaveMsgVo> getGoodsTop10List(String goodsId, String authorization) {
        validateAuthorization(authorization);
        if (!StringUtils.hasText(goodsId)) {
            throw new IllegalArgumentException("商品ID不能为空");
        }
        return vtpCustomerLeaveMsgMapper.selectGoodsTop10List(goodsId);
    }

    @Override
    public List<VtpCustomerLeaveMsgVo> getGoodsTop2List(String goodsId, String authorization) {
        validateAuthorization(authorization);
        if (!StringUtils.hasText(goodsId)) {
            throw new IllegalArgumentException("商品ID不能为空");
        }
        return vtpCustomerLeaveMsgMapper.selectGoodsTop2List(goodsId);
    }

    @Override
    public VtpCustomerLeaveMsgPageResponse getGoodsMoreList(Long objectId, String objectType, PageQuery pageQuery, String authorization) {
        validateAuthorization(authorization);
//        if (objectId == null) {
//            throw new IllegalArgumentException("查询商品留言时objectId不能为空");
//        }
//        if (!"goods".equals(objectType)) {
//            throw new IllegalArgumentException("查询商品留言时objectType必须为goods");
//        }

        return executePageQuery(page ->
                        vtpCustomerLeaveMsgMapper.selectGoodsMoreList(page, objectId, objectType),
                pageQuery
        );
    }

    @Override
    public TableDataInfo<VtpCustomerLeaveMsgVo> getPlatformList(String objectType, PageQuery pageQuery) {
        // 验证objectType参数
        if (objectType != null && !"plat".equals(objectType)) {
            throw new IllegalArgumentException("查询平台留言时objectType必须为plat或null");
        }

        // 确保分页参数有效
        Page<VtpCustomerLeaveMsgVo> page = new Page<>(
                pageQuery.getPageNum() != null ? pageQuery.getPageNum() : 1,
                pageQuery.getPageSize() != null ? pageQuery.getPageSize() : 10
        );

        // 执行查询
        IPage<VtpCustomerLeaveMsgVo> result = vtpCustomerLeaveMsgMapper.selectPlatformList(page, "plat");

        // 构建分页响应
        return TableDataInfo.build(result);
    }


    /**
     * 执行分页查询的公共方法
     */
    private VtpCustomerLeaveMsgPageResponse executePageQuery(PageQueryFunction function, PageQuery pageQuery) {
        Page<VtpCustomerLeaveMsgVo> page = new Page<>(
                pageQuery.getPageNum() != null ? pageQuery.getPageNum() : 1,
                pageQuery.getPageSize() != null ? pageQuery.getPageSize() : 10
        );

        Page<VtpCustomerLeaveMsgVo> result = function.execute(page);
        VtpCustomerLeaveMsgPageResponse response = new VtpCustomerLeaveMsgPageResponse();
//        response.setRows(result.getRecords());
        response.setTotal(result.getTotal()); // 将 int 转换为 Long
        return response;
    }

    /**
     * 分页查询函数式接口
     */
    @FunctionalInterface
    private interface PageQueryFunction {
        Page<VtpCustomerLeaveMsgVo> execute(Page<VtpCustomerLeaveMsgVo> page);
    }

    /**
     * 验证授权信息
     */
    private void validateAuthorization(String authorization) {
        // 实际项目中应实现授权验证逻辑
        if (!StringUtils.hasText(authorization)) {
            throw new IllegalArgumentException("缺少授权信息");
        }
        // 可以添加token解析和验证逻辑
    }

    @Override
    @Transactional
    public int saveLeaveMsg(VtpCustomerLeaveMsgDto dto, Integer wxUserId, String nickName, String avatar) {
        // 验证留言类型规则
        if (!validateMsgType(dto.getObjectType(), dto.getMsgType())) {
            throw new IllegalArgumentException("留言类型不符合规则");
        }

        // 验证平台留言的特殊规则
        if ("plat".equals(dto.getObjectType())) {
            dto.setObjectId(null); // 平台留言对象ID必须为null
        }

        // DTO转实体
        VtpCustomerLeaveMsg entity = new VtpCustomerLeaveMsg();
        BeanUtils.copyProperties(dto, entity);

        // 设置用户信息
        entity.setWxUserId(wxUserId.longValue());
        entity.setNickName(nickName);
        entity.setAvatar(avatar);

        // 插入数据库
        return vtpCustomerLeaveMsgMapper.insertLeaveMsg(entity);
    }

    /**
     * 验证留言类型规则
     * 平台留言: msgType可以是img或txt
     * 商品留言: msgType必须是txt
     */
    private boolean validateMsgType(String objectType, String msgType) {
        if ("goods".equals(objectType)) {
            return "txt".equals(msgType);
        }
        return "img".equals(msgType) || "txt".equals(msgType);
    }


}
